using UnityEngine;
using QFramework;
using Unity.Mathematics;
using UnityEngine.SocialPlatforms;
using System;

namespace QFramework.FlappyBird
{
	public partial class PipeGenarator : ViewController
	{
		private float generateTime = 0;
		public float MaxDuration = 2.5f;
		public float MinDuration = 1f;
		private float duration;

		public static SimpleObjectPool<Pipe> PipePool;

		void Start()
		{
			// Code Here
			Pipe.Hide();

			generateTime = Time.time;

			duration = UnityEngine.Random.Range(MinDuration, MaxDuration);

			PipePool = new SimpleObjectPool<Pipe>(() =>Pipe.Instantiate().Hide(),
				(pipe) => { pipe.ResetData(); },20);
		}

        private void Update()
        {	
			if(FlappyBird.GameState.Value == GameStates.NotStart)
			{
				return;
			}

            if(Time.time - generateTime > duration)
			{	
				
				PipePool.Allocate().Position(PipeGenaratePos.position)
					.PositionY(UnityEngine.Random.Range(-5.5f,0f))
					.Show();

				generateTime = Time.time;

                duration = UnityEngine.Random.Range(MinDuration, MaxDuration);
            }
        }
    }
}
